package util
{
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	
	import model.siw.CR;
	import model.siw.Rule;
	import model.siw.RuleFigure;
	import model.siw.SIW;
	import model.siw.TR;
	import model.siw.TranFigure;
	
	import mx.collections.ArrayList;
	import mx.containers.Canvas;
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.controls.ButtonLabelPlacement;
	import mx.controls.Label;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	public class TimeRelationWindow extends TitleWindow
	{
		private var canvas:Canvas;
		
		private var prompt:Label;
		
		private var okBtn:Button;
		
		private var cancelBtn:Button;
		
		private var siw:SIW;
		
		private var ruleFigures:ArrayList;
		
		private var tranFigures:ArrayList;
		
		private var startX:int=10;
		
		private var startY:int=20;
		
		private var col:int=0;
		
		private var ruleWidth:int=0;
		
		private var ruleHeight:int=0;
		
		public function TimeRelationWindow(){
			
			siw=SIW.createInstance();
			
			ruleFigures=new ArrayList();
			
			tranFigures=new ArrayList();
			
			var i:int=0;
			
			for(i=0;i<siw.rules.length;i++){
				
				var ruleFigure:RuleFigure=new RuleFigure(i, Rule(siw.rules.getItemAt(i)).type);				
				ruleFigure.model=Rule(siw.rules.getItemAt(i));
				ruleFigure.addEventListener(MouseEvent.MOUSE_DOWN, pickup);
				ruleFigure.addEventListener(MouseEvent.MOUSE_UP,place)
				ruleFigures.addItem(ruleFigure);
				
				if(i!=siw.rules.length-1){
					var tranFigure:TranFigure=new TranFigure();
					tranFigures.addItem(tranFigure);
				}
				
			}
			
			this.width=520;
			this.height=250;
			this.layout="absolute";
			
			this.showCloseButton=true;
            this.addEventListener(CloseEvent.CLOSE, onClose);
            
            prompt=new Label();
            prompt.text="拖动选择时序, 右键设置关联:";
            prompt.x=10;
            prompt.y=10;
            prompt.width=200;
            prompt.height=20;
            
            canvas=new Canvas();
            canvas.x=prompt.x;
            canvas.y=prompt.y+prompt.height+15;
            canvas.width=470;
            canvas.height=120;
            
            col=(canvas.width-20)/(ruleFigures.length-0.5);
            ruleWidth=col*0.5;
            ruleHeight=60;
            
            okBtn=new Button();
			
			okBtn.x=canvas.x;
			okBtn.y=canvas.y+canvas.height+10;
			okBtn.width=80;
			okBtn.height=20;
			okBtn.label="确认";
			
			cancelBtn=new Button();
			
			cancelBtn.x=canvas.x+canvas.width-80;
			cancelBtn.y=canvas.y+canvas.height+10;
			cancelBtn.width=80;
			cancelBtn.height=20;
			cancelBtn.label="取消";
			
			okBtn.addEventListener(MouseEvent.CLICK, onFinish);
			cancelBtn.addEventListener(MouseEvent.CLICK, onCancel);
			this.addChild(prompt);
			this.addChild(canvas);
			this.addChild(okBtn);
			this.addChild(cancelBtn);			
            
            initRules();
			
		}
		
		private function initRules(){
			var i:int;
			var oldX:int=startX+ruleWidth;
			var newX:int=oldX;
			var y:int=startY+ruleHeight/2;
			var oldTranFigure:TranFigure=null;
			
			for(i=0;i<ruleFigures.length-1;i++){
				var newTranFigure:TranFigure=TranFigure(tranFigures.getItemAt(i));
				newTranFigure.x=startX+col*i;
				newTranFigure.y=2;
				newTranFigure.width=col+ruleWidth;
				newTranFigure.height=ruleHeight+20*2;
				newTranFigure.col=this.col;
				canvas.addChild(newTranFigure);
			}
			
			for(i=0;i<ruleFigures.length;i++){
				
				var ruleFigure:RuleFigure=RuleFigure(ruleFigures.getItemAt(i));
				ruleFigure.width=ruleWidth;
				ruleFigure.height=ruleHeight;
				ruleFigure.x=startX+col*ruleFigure.position;
				ruleFigure.y=startY;
				ruleFigure.label=ruleFigure.model.et.topic;
				ruleFigure.toolTip="Type: "+ruleFigure.model.type+"\nService: "+ruleFigure.model.service.name+"\nOp: "+ruleFigure.model.op.name+"\nET: "+ruleFigure.model.et.topic;
				ruleFigure.labelPlacement=ButtonLabelPlacement.TOP;
				canvas.addChild(ruleFigure);
				newX=ruleFigure.x+ruleFigure.width;
				if(newX!=oldX){
					//drawLineDepend(oldX, newX-ruleWidth, y);
					oldX=newX;
				}
				
				if(i!=ruleFigures.length-1){
					if(oldTranFigure!=null){
						oldTranFigure.newRuleFigure=ruleFigure;
						ruleFigure.inputTran=oldTranFigure;	
					}
					var newTranFigure:TranFigure=TranFigure(tranFigures.getItemAt(i));
					newTranFigure.oldRuleFigure=ruleFigure;
					ruleFigure.outputTran=newTranFigure;					
					oldTranFigure=newTranFigure;
				}else{
					oldTranFigure.newRuleFigure=ruleFigure;
					ruleFigure.inputTran=oldTranFigure;	
				}			
				
			}
		}
		
		/** private function drawLineDefault(oldX:int, newX:int, y){
			
			canvas.graphics.lineStyle(2,0x000000);
			canvas.graphics.moveTo(oldX, y);
			canvas.graphics.lineTo(newX-6, y);
			
			canvas.graphics.beginFill(0xFF0000);
			canvas.graphics.moveTo(newX,y);
			canvas.graphics.lineTo(newX-6,y-3);
			canvas.graphics.lineTo(newX-6,y+3);
			canvas.graphics.lineTo(newX,y);
			canvas.graphics.endFill();	
			
		}
		
		private function drawLineCircle(oldX:int, newX:int, y){
			
			canvas.graphics.lineStyle(2,0x000000);
			canvas.graphics.moveTo(oldX, y);
			canvas.graphics.lineTo((oldX+newX)/2-3, y);	
			canvas.graphics.moveTo((oldX+newX)/2+3, y);
			canvas.graphics.lineTo(newX-6, y);			
			
			canvas.graphics.beginFill(0xFF0000);
			canvas.graphics.moveTo(newX,y);
			canvas.graphics.lineTo(newX-6,y-3);
			canvas.graphics.lineTo(newX-6,y+3);
			canvas.graphics.lineTo(newX,y);
			canvas.graphics.endFill();	
			
			canvas.graphics.drawCircle((oldX+newX)/2, y, 3);
		}
		
		private function drawLineDepend(oldX:int, newX:int, y){
			
			canvas.graphics.lineStyle(2,0x000000);
			canvas.graphics.moveTo(oldX, y);
			canvas.graphics.lineTo((oldX+newX)/2-5, y);	
			canvas.graphics.moveTo((oldX+newX)/2+5, y);
			canvas.graphics.lineTo(newX-6, y);			
			
			canvas.graphics.beginFill(0x000000);
			canvas.graphics.moveTo(newX,y);
			canvas.graphics.lineTo(newX-6,y-3);
			canvas.graphics.lineTo(newX-6,y+3);
			canvas.graphics.lineTo(newX,y);
			canvas.graphics.endFill();	
			
			canvas.graphics.drawCircle((oldX+newX)/2, y, 5);			
				
			dranDashedLine(oldX-ruleWidth/2,newX+ruleWidth/2,startY);	
			
		}
		
		private function dranDashedLine(oldX:int, newX:int, y){
			var space:int=2;
			var dashed:int=4;
			var i:int;
			
			canvas.graphics.lineStyle(1,0x000000);
			
			for(i=oldX;i<newX;){				
				canvas.graphics.moveTo(i, y-15);
				canvas.graphics.lineTo(i+dashed,y-15);
				i+=(dashed+space);				
			}
			
			for(i=y-15;i<y;){				
				canvas.graphics.moveTo(oldX, i);
				canvas.graphics.lineTo(oldX, i+dashed);				
				canvas.graphics.moveTo(newX, i);
				canvas.graphics.lineTo(newX, i+dashed);				
				i+=(dashed+space);	
			}
			
			canvas.graphics.beginFill(0x000000);
			canvas.graphics.moveTo(oldX,y);
			canvas.graphics.lineTo(oldX+3,y-6);
			canvas.graphics.lineTo(oldX-3,y-6);
			canvas.graphics.lineTo(oldX,y);
			canvas.graphics.endFill();	
			
		} **/
		
		private function onClose(e:CloseEvent){
			PopUpManager.removePopUp(this);
		}
		
		private function onFinish(e:MouseEvent){
			
			/**
			 * 生成siwxml.
			 * */
			
			var siw:SIW=SIW.createInstance();
			 
			var i:int;
			
			for(i=0;i<tranFigures.length;i++){
				var tran:TranFigure=TranFigure(tranFigures.getItemAt(i));
				if(tran.tranType!="non"){
					var tr:TR=new TR();
					tr.pre=tran.oldRuleFigure.model.id;
					tr.post=tran.newRuleFigure.model.id;
					tr.op=tran.tranType;
					siw.trs.addItem(tr);
					
					var cr:CR=new CR();
					cr.pre=tran.oldRuleFigure.model.id;
					cr.post=tran.newRuleFigure.model.id;
					var f:String;
					var l:String;
					if(tran.oldRuleFigure.type=="etr"){
						f="t";
					}else{
						f="l";
					}
					if(tran.newRuleFigure.type=="etr"){
						l="t";
					}else{
						l="l";
					}
					cr.type=f+"_"+l;
					siw.crs.addItem(cr);
					
				}
			}
			 
			PopUpManager.removePopUp(this);
			
		}
		
		private function onCancel(e:MouseEvent){
			PopUpManager.removePopUp(this);
		}
		
		public function pickup(event:MouseEvent):void{
			
			event.target.startDrag();
			event.target.filters=[new DropShadowFilter()];
			
		}

		public function place(event:MouseEvent):void{
			
			var sourceRule:RuleFigure;
			var targetRule:RuleFigure;
			
			sourceRule=RuleFigure(event.target);
			
			event.target.stopDrag();
			event.target.filters=null;
			var i:int;
			var sery:int=sourceRule.position;
			var endX:int=event.target.x;
			for(i=0;i<ruleFigures.length;i++){
				var min=startX+i*col;
				var max=min+ruleWidth;
				if(endX>=min && endX<=max){
					sery=i;
					break;
				}
			}
			sourceRule.x=startX+sery*col;
			sourceRule.y=startY;
			
			if(sery!=sourceRule.position){
				for(i=0;i<ruleFigures.length;i++){
					targetRule=RuleFigure(ruleFigures.getItemAt(i));
					if(targetRule.position==sery){
						targetRule.x=startX+sourceRule.position*col;
						targetRule.position=sourceRule.position;
						sourceRule.position=sery;
						
						/*update transitions info*/
						
						var sourceInputTran:TranFigure=sourceRule.inputTran;
						var sourceOutputTran:TranFigure=sourceRule.outputTran;
						var targetInputTran:TranFigure=targetRule.inputTran;
						var targetOutputTran:TranFigure=targetRule.outputTran;
						
						if(sourceInputTran!=null){
							sourceInputTran.newRuleFigure=targetRule;
						}
						
						if(sourceOutputTran!=null){
							sourceOutputTran.oldRuleFigure=targetRule;
						}
						
						if(targetInputTran!=null){
							targetInputTran.newRuleFigure=sourceRule;
						}
						
						if(targetOutputTran!=null){
							targetOutputTran.oldRuleFigure=sourceRule;
						}
						
						sourceRule.inputTran=targetInputTran;
						sourceRule.outputTran=targetOutputTran;
						
						targetRule.inputTran=sourceInputTran;
						targetRule.outputTran=sourceOutputTran;
						
						/*----------------------------*/
						
						break;
					}
				}
			}
			
			
			
		}

	}
}